<?php
namespace app\index\controller;

use app\common\model\Cate;
use app\common\model\Navigation;
use app\common\model\User;
use app\common\model\UserToken;
use think\Controller;

class Index extends Controller
{
    private $uid =1;
    protected function initialize()
    {

        parent::initialize();
        //判断有没有传递uid 没有默认为1
        if(empty(input('uid'))){
            $this->uid = 1;
        }else{
            $userTokenModel = new UserToken();
            $result = $userTokenModel->checkToken(input('uid'));
            if(!$result['code']){
                $this->uid = 1;
            }else{
                $this->uid = $result['data']['user_id'];
            }
        }

    }
    public function index()
    {
        //判断有没有传递uid 没有默认为1
//        $uid = input('uid','1');
        $um = new User();
        $username =$um->where('id',$this->uid)->value('username');
        $nm= new Navigation();
        $url_list =$nm->where('uid',$this->uid)->select()->toArray();
        //获取分类数据
        $cm = new Cate();
        $cateinfo  = $cm->where('uid',$this->uid)->column('cate_name','id');
        $url_listinfo = [];
        //相同value 组成新的一个二维数组
        foreach($url_list as $k=>$v){
            $url_listinfo[$v['cate_name']][]    =   $v;
        }
        $url = [];
        foreach ($url_listinfo as $kk=>&$vv){
            foreach ($vv as $c=>$sk){
                if(array_key_exists($sk['cate_name'],$cateinfo)){
                    $url[$kk]['cate_name'] = $cateinfo[$sk['cate_name']];
                }
            }
            $url[$kk]['urls'] = $vv;
        }
        $this->assign('username',$username);
        $this->assign('uid',$this->uid);
        $this->assign('url_list',$url);
        
        
        return $this->fetch();
    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }
}
